\chapter{Data Trace Encoder Output Packets} \label{dataTracePackets}

Data trace packets must be differentiated from instruction trace packets, and the means by which
this is accomplished is dependent on the trace transport infrastructure.  Several possibilities exist:
One option is for instruction and data trace to be issued using different IDs (for example, if using ATB transport,
different \textbf{ATID} values).  Alternatively, an additional field as part of the packet encapsulation can be 
used (Siemens uses a 2-bit \textbf{msg\_type} field to differentiate different trace types from the same source).

By default, all data trace packets include both address and data.  However, provision is made for run-time 
configuration options to exclude either the address or the data, in order to minimize trace bandwidth.  For example,
if filtering has been configured to only trace from a specific data access address there is no need to report
the address in the trace.  Alternatively, the user may want to know which locations are accessed but not care 
about the data value. Information about whether address or data are omitted is not encoded in the packets
themselves as it does not change dynamically, and to do so would reduce encoding efficiency.  The run-time
configuration should be reported in the Format 3, subformat 3 support packet (see section~\ref{sec:format33}).
The following sections include examples for all three cases.

As outlined in section~\ref{sec:DataInterfaceRequirements}, two different signaling protocols
between the RISC-V hart and the encoder are supported: \textit{unified} and \textit{split}.
Accordingly, both unified and split trace packets are defined.

Note: in the following tables, "clog2" is an abbreviation for "ceiling of log2".

\section{Load and Store} \label{sec:data-loadstore}

\begin{table}[htp]
  \centering
  \caption{Packet format for Unified load or store, with address and data}
  \label{tab:te_datadx0y0}
  \begin{tabulary}{\textwidth}{|l|p{38mm}|p{92mm}|}
    \hline
    {\bf Field name} & {\bf Bits} & {\bf Description} \\
    \hline
    \textbf{format} & 	2 or 3	& Transaction type\newline
                000: Unified load or split load address, aligned\newline
                001: Unified load or split load address, unaligned\newline
		010: Store, aligned address\newline	
		011: Store, unaligned address\newline	
		(other codes select other packet formats)\\
    \hline
    \textbf{size} & max(1, clog2(clog2( \textit{data\_width\_p}/8 + 1)) & Transfer size is 2\textsuperscript{\textbf{size}} bytes \\
    \hline
    \textbf{diff} & 2 & 00: Full address and data (sync)\newline
		        01: Differential address, XOR-compressed data\newline
		        10: Differential address, full data\newline
		        11: Differentail address, differential data\\
    \hline
    \textbf{data\_len}	& \textbf{size} & Number of bytes of data is \textbf{data\_len} + 1	\\
    \hline
    \textbf{data} & 8 * (\textbf{data\_len} + 1) & 
                Data\\
    \hline
    \textbf{address} &  \textit{daddress\_width\_p} & Byte address if format is unaligned, otherwise shift left by \textbf{size} to recover byte address \\
    \hline
  \end{tabulary}
\end{table}


\begin{table}[htp]
  \centering
  \caption{Packet format for Unified load or store, with address only}
  \label{tab:te_datadx0y1}
  \begin{tabulary}{\textwidth}{|l|p{38mm}|p{92mm}|}
    \hline
    {\bf Field name} & {\bf Bits} & {\bf Description} \\
    \hline
    \textbf{format} & 	2 or 3	& Transaction type\newline
		000: Unified load or split load address, aligned\newline	
		001: Unified load or split load address, unaligned\newline
		010: Store, aligned address\newline
		011: Store, unaligned address\newline
		(other codes select other packet formats)\\
    \hline
    \textbf{size} & max(1, clog2(clog2( \textit{data\_width\_p}/8 + 1)) & Transfer size is 2\textsuperscript{\textbf{size}} bytes \\
    \hline
    \textbf{diff} & 1 & 0: Full address (sync)	\newline
		        1:  Differential address\\
    \hline
    \textbf{address} &  \textit{daddress\_width\_p} & Byte address if format is unaligned, otherwise shift left by \textbf{size} to recover byte address \\
    \hline
  \end{tabulary}
\end{table}


\begin{table}[htp]
  \centering
  \caption{Packet format for Unified load or store, with data only}
  \label{tab:te_datadx0y2}
  \begin{tabulary}{\textwidth}{|l|p{38mm}|p{92mm}|}
    \hline
    {\bf Field name} & {\bf Bits} & {\bf Description} \\
    \hline
    \textbf{format} & 	2 or 3	& Transaction type\newline
		000: Unified load or split load address, aligned\newline	
		001: Unified load or split load address, unaligned\newline
		010: Store, aligned address\newline
		011: Store, unaligned address\newline
		(other codes select other packet formats)\\
    \hline
    \textbf{size} & max(1, clog2(clog2( \textit{data\_width\_p}/8 + 1)) & Transfer size is 2\textsuperscript{\textbf{size}} bytes \\
    \hline
    \textbf{diff} & 1 or 2 & 00: Full data (sync)\newline
		             01: Compressed data (XOR if 2 bits) \newline
		             10: reserved \newline
		             11: Differential data\\
    \hline
    \textbf{data} & \textit{data\_width\_p} & 
                Data\\
    \hline
  \end{tabulary}
\end{table}


\begin{table}[htp]
  \centering
  \caption{Packet format for Split load - Address only}
  \label{tab:te_datadx0y3}
  \begin{tabulary}{\textwidth}{|l|p{38mm}|p{92mm}|}
    \hline
    {\bf Field name} & {\bf Bits} & {\bf Description} \\
    \hline
    \textbf{format} & 	3	& Transaction type\newline
		000: Unified load or split load address, aligned\newline	
		001: Unified load or split load address, unaligned\newline
		(other codes select other packet formats)\\
    \hline
    \textbf{size} & max(1, clog2(clog2( \textit{data\_width\_p}/8 + 1)) & Transfer size is 2\textsuperscript{\textbf{size}} bytes \\
    \hline
    \textbf{index} & \textit{index\_width\_p} & Transfer index\\
    \hline
    \textbf{diff} & 1 & 0: Full address (sync)	\newline
		        1: Differential address\\
    \hline
    \textbf{address} &  \textit{daddress\_width\_p} & Byte address if format is unaligned, otherwise shift left by \textbf{size} to recover byte address \\
    \hline
  \end{tabulary}
\end{table}

\begin{table}[htp]
  \centering
  \caption{Packet format for Split load - Data only}
  \label{tab:te_datadx0y4}
  \begin{tabulary}{\textwidth}{|l|p{38mm}|p{92mm}|}
    \hline
    {\bf Field name} & {\bf Bits} & {\bf Description} \\
    \hline
    \textbf{format} & 	3	& Transaction type\newline
		100: split load data\newline	
		(other codes select other packet formats)\\
    \hline
    \textbf{size} & max(1, clog2(clog2( \textit{data\_width\_p}/8 + 1)) & Transfer size is 2\textsuperscript{\textbf{size}} bytes \\
    \hline
    \textbf{index} & \textit{index\_width\_p} & Transfer index\\
    \hline
    \textbf{resp} & 	2	& 
		00: Error (no data)\newline	
		01: XOR-compressed data\newline	
		10: Full data\newline	
		11: Differential data\\
    \hline
    \textbf{data} & \textit{data\_width\_p} & 
                Data\\
    \hline
  \end{tabulary}
\end{table}

\subsection{format field} \label{sec:loadstore-format}

Types of data trace packets are differentiated by the \textbf{format} field.  This field is 2 bits wide if
only unified loads and stores are supported, or 3 bits otherwise.

Unified loads and split load request phase share the same code because the encoder will support one or the 
other, indicated by a discoverable parameter.

Data accesses aligned to their size (e.g. 32-bit loads aligned to 32-bit word boundaries) are expected to
be commonplace, and in such cases, encoding efficiency can be improved by not reporting the redundant LSBs of the address.

\subsection{size field} \label{sec:loadstore-size}

The width of this field is 2 bits if max size is 64-bits (\textit{data\_width\_p} < 128), 3 bits if wider.

\subsection{diff field} \label{sec:loadstore-diff}

Unlike instruction trace, compression options for data trace are somewhat limited.
Following a synchronization instruction trace packet, the first data trace packet for a given access size must 
include the full (unencoded) data access address.  Thereafter, the address may be reported differentially (i.e. 
address of this data access, minus the address of the previous data access of the same size). 

Similarly, following a synchronization instruction trace packet, the first data trace packet for a given access size 
must include the full (unencoded) data value.  Beyond this, data may be encoded or unencoded depending on whichever
results in the most efficient represenation.  Implementors may chose to offer one of XOR or differential compression,
or both.  XOR compression will be simpler to implement, and avoids the need for performing subtraction of large 
values.

If only one data compression type is offered, the \textbf{diff} field can be 1 bit wide rather than 2 for table \ref{tab:te_datadx0y2}.

\subsection{data\_len field} \label{sec:loadstore-datalen}

However the data is compressed, upper bytes that are all the same value do not need to be 
included in the packet; the decoder can recreate the full-width value by sign extending from the most significant 
received bit.  In cases where \textbf{data} is not the final field in the packet, the width of \textbf{data} is 
indicated by this field.

\FloatBarrier
\section{Atomic} \label{sec:data-atomic}

\begin{table}[htp]
  \centering
  \caption{Packet format for Unified atomic with address and data}
  \label{tab:te_datadx0y5}
  \begin{tabulary}{\textwidth}{|l|p{38mm}|p{92mm}|}
    \hline
    {\bf Field name} & {\bf Bits} & {\bf Description} \\
    \hline
    \textbf{format} & 	3	& Transaction type\newline 	
		110: Unified atomic or split atomic address\newline	
		(other codes other packet formats)\\
    \hline
    \textbf{subtype} & 	3	& Atomic sub-type\newline
	        000: Swap\newline
		001: ADD\newline	
		010: AND\newline	
		011: OR\newline	
		100: XOR\newline	
		101: MAX\newline	
		110: MIN\newline	
		111: reserved\\	

    \hline
    \textbf{size} & max(1, clog2(clog2( \textit{data\_width\_p}/8 + 1)) & Transfer size is 2\textsuperscript{\textbf{size}} bytes\\
    \hline
    \textbf{diff} & 2 & 00: Full address and data (sync)\newline
		01: Differential address, XOR-compressed data\newline
		10: Differential address, full data\newline
		11: Differential address, differential data\\
    \hline
    \textbf{op\_len} & \textbf{size} & Number of bytes of operand is \textbf{op\_len} + 1\\
    \hline
    \textbf{operand}	& 8 * (\textbf{op\_len} + 1) & Operand.  Value from rs2 before operator applied\\
    \hline
    \textbf{data\_len}	& \textbf{size} & Number of bytes of data is \textbf{data\_len} + 1\\
    \hline
    \textbf{data} & 8 * (\textbf{data\_len} + 1) & 
                Data\\
    \hline
    \textbf{address} &  \textit{daddress\_width\_p} & Address, aligned and encoded as per size \\
    \hline
  \end{tabulary}
\end{table}

\begin{table}[htp]
  \centering
  \caption{Packet format for Unified atomic with address only}
  \label{tab:te_datadx0y6}
  \begin{tabulary}{\textwidth}{|l|p{38mm}|p{92mm}|}
    \hline
    {\bf Field name} & {\bf Bits} & {\bf Description} \\
    \hline
    \textbf{format} & 	3	& Transaction type\newline 	
		110: Unified atomic or split atomic address\newline	
		(other codes other packet formats)\\
    \hline
    \textbf{subtype} & 	3	& Atomic sub-type\newline
	        000: Swap\newline
		001: ADD\newline	
		010: AND\newline	
		011: OR\newline	
		100: XOR\newline	
		101: MAX\newline	
		110: MIN\newline	
		111: conditional store failure\\	
    \hline
    \textbf{size} & max(1, clog2(clog2( \textit{data\_width\_p}/8 + 1)) & Transfer size is 2\textsuperscript{\textbf{size}} bytes\\
    \hline
    \textbf{diff} & 1 & 0: Full address \newline
		1: Differential address\\
    \hline
    \textbf{address} &  \textit{daddress\_width\_p} & Address, aligned and encoded as per size \\
    \hline
  \end{tabulary}
\end{table}


\begin{table}[htp]
  \centering
  \caption{Packet format for Unified atomic with data only}
  \label{tab:te_datadx0y7}
  \begin{tabulary}{\textwidth}{|l|p{38mm}|p{92mm}|}
    \hline
    {\bf Field name} & {\bf Bits} & {\bf Description} \\
    \hline
    \textbf{format} & 	3	& Transaction type\newline 	
		110: Unified atomic or split atomic address\newline	
		(other codes other packet formats)\\
    \hline
    \textbf{subtype} & 	3	& Atomic sub-type\newline
	        000: Swap\newline
		001: ADD\newline	
		010: AND\newline	
		011: OR\newline	
		100: XOR\newline	
		101: MAX\newline	
		110: MIN\newline	
		111: reserved\\	
    \hline
    \textbf{size} & max(1, clog2(clog2( \textit{data\_width\_p}/8 + 1)) & Transfer size is 2\textsuperscript{\textbf{size}} bytes\\
    \hline
    \textbf{diff} & 1 or 2 & 00: Full data (sync)\newline
		             01: Compressed data (XOR if 2 bits) \newline
		             10: reserved \newline
		             11: Differential data\\
    \hline
    \textbf{op\_len} & \textbf{size} & Number of bytes of operand is \textbf{op\_len} + 1\\
    \hline
    \textbf{operand}	& 8 * (\textbf{op\_len} + 1) & Operand.  Value from rs2 before operator applied\\
    \hline
    \textbf{data} & \textit{data\_width\_p} & 
                Data\\
    \hline
  \end{tabulary}
\end{table}


\begin{table}[htp]
  \centering
  \caption{Packet format for Split atomic with operand only}
  \label{tab:te_datadx0y8}
  \begin{tabulary}{\textwidth}{|l|p{38mm}|p{92mm}|}
    \hline
    {\bf Field name} & {\bf Bits} & {\bf Description} \\
    \hline
    \textbf{format} & 	3	& Transaction type\newline 	
		110: Unified atomic or split atomic address\newline	
		(other codes other packet formats)\\
    \hline
    \textbf{subtype} & 	3	& Atomic sub-type\newline
	        000: Swap\newline
		001: ADD\newline	
		010: AND\newline	
		011: OR\newline	
		100: XOR\newline	
		101: MAX\newline	
		110: MIN\newline	
		111: reserved\\	
    \hline
    \textbf{size} & max(1, clog2(clog2( \textit{data\_width\_p}/8 + 1)) & Transfer size is 2\textsuperscript{\textbf{size}} bytes bytes\\
    \hline
    \textbf{index} & \textit{index\_length\_p} & Transfer index\\
    \hline
    \textbf{diff} & 1 or 2 & 00: Full address and data (sync)\newline
		01:  Differential address, XOR-compressed data\newline
		10: Differential address, full data\newline
		11: Differential address, differential data\\
    \hline
    \textbf{op\_len} & \textbf{size} &	Number of bytes of operand is \textbf{op\_len} + 1\\
    \hline
    \textbf{operand}	& 8 * (\textbf{op\_len} + 1) & Operand.  Value from rs2 before operator applied\\
    \hline
    \textbf{address} &  \textit{daddress\_width\_p} &Address, aligned and encoded as per size \\
    \hline
  \end{tabulary}
\end{table}

\begin{table}[htp]
  \centering
  \caption{Packet format for Split atomic load data only}
  \label{tab:te_datadx0y9}
  \begin{tabulary}{\textwidth}{|l|p{38mm}|p{92mm}|}
    \hline
    {\bf Field name} & {\bf Bits} & {\bf Description} \\
    \hline
    \textbf{format} & 	3	& Transaction type\newline 	
		111: Split atomic data\newline	
		other codes other packet formats\\
    \hline
    \textbf{index} & \textit{index\_length\_p} & Transfer index\\
    \hline
    \textbf{resp} & 2 & 00: Error (no data)\newline
		01:  XOR-compressed data\newline	
		10: full data\newline
		11: differential data\\
    \hline
    \textbf{data\_len} & \textbf{size} &	Number of bytes of operand is \textit{data\_len + 1}. Not included if resp indicates an error (sign-extend \textbf{resp} MSB)\\
    \hline
    \textbf{data} &  8 * (\textbf{data\_len} + 1) & Data. Not included if resp indicates an error (sign-extend \textbf{resp} MSB)\\
    \hline
  \end{tabulary}
\end{table}

\subsection{size field} \label{sec:atomic-size}

Strictly, \textbf{size} could be just one bit as atomics are currently either 32 or 64 bits.  
Defining as per regular loads and stores provisions for future extensions (proprietary or otherwise) that support smaller atomics.

\subsection{diff field} \label{sec:atomic-diff}

See section~\ref{sec:loadstore-diff}.

\subsection{operand field} \label{sec:atomic-operand}

The operand value for the atomic operation.  Uncompressed, although upper bytes 
that are all the same value do not need to be included in the packet; the decoder can recreate the 
full-width value by sign extending from the most significant received bit; see section \ref{sec:atomic-datalen}.

\subsection{data\_len and op\_len fields} \label{sec:atomic-datalen}

Width of \textbf{data and \textbf{operand} fields respectively.
See section \ref{sec:loadstore-datalen}.}

\FloatBarrier
\section{CSR} \label{sec:data-csr}

\begin{table}[htp]
  \centering
  \caption{Packet format for Unified CSR, with address, data and operand}
  \label{tab:te_datadx0y10}
  \begin{tabulary}{\textwidth}{|l|p{38mm}|p{92mm}|}
    \hline
    {\bf Field name} & {\bf Bits} & {\bf Description} \\
    \hline
    \textbf{format} & 	3	& Transaction type\newline 	
		101: CSR\newline
		(other codes other packet formats)\\
    \hline
    \textbf{subtype} & 	2	& CSR sub-type\newline
		00: RW\newline	
		01: RS\newline	
		10: RC\newline	
		11: reserved\\	
    \hline
    \textbf{diff} & 1 or 2 & 00: Full data (sync)\newline
		             01: Compressed data (XOR if 2 bits) \newline
		             10: reserved \newline
		             11: Differential data\\
    \hline
    \textbf{data\_len}	& 2 or 3 & Number of bytes of data is \textbf{data\_len} + 1\\
    \hline
    \textbf{data} & 8 * (\textbf{data\_len} + 1) & Data\\
    \hline
    \textbf{addr\_msbs} & 6  &	Address[11:6]\\
    \hline
    \textbf{op\_len} & 2 or 3  & Number of bytes of operand is \textbf{op\_len} + 1\\    
    \hline
    \textbf{operand}	& 8 * (\textbf{op\_len} + 1) & Operand.  Value from rs1 before operator applied\\
    \hline
    \textbf{addr\_lsbs} &  6 & Address[5:0] \\
    \hline
  \end{tabulary}
\end{table}

\begin{table}[htp]
  \centering
  \caption{Packet format for Unified CSR, with address and read-only data (as determined by addr[11:10] = 11)}
  \label{tab:te_datadx0y11}
  \begin{tabulary}{\textwidth}{|l|p{38mm}|p{92mm}|}
    \hline
    {\bf Field name} & {\bf Bits} & {\bf Description} \\
    \hline
    \textbf{format} & 	3	& Transaction type\newline 	
		101: CSR\newline
		other codes other packet formats\\
    \hline
    \textbf{subtype} & 	2	& CSR sub-type\newline
		00: RW\newline	
		01: RS\newline	
		10: RC\newline	
		11: reserved\\	
    \hline
    \textbf{diff} & 1 or 2 & 00: Full data (sync)\newline
		             01: Compressed data (XOR if 2 bits) \newline
		             10: reserved \newline
		             11: Differential data\\
    \hline
    \textbf{data\_len}	& 2 or 3 & Number of bytes of data is \textbf{data\_len} + 1\\
    \hline
    \textbf{data} & 8 * (\textbf{data\_len} + 1) & Data\\
    \hline
    \textbf{addr\_msbs} & 6  &	Address[11:6]\\
    \hline
    \textbf{addr\_lsbs} &  6 & Address[5:0] \\
    \hline
  \end{tabulary}
\end{table}

\begin{table}[htp]
  \centering
  \caption{Packet format for Unified CSR, with address only}
  \label{tab:te_datadx0y12}
  \begin{tabulary}{\textwidth}{|l|p{38mm}|p{92mm}|}
    \hline
    {\bf Field name} & {\bf Bits} & {\bf Description} \\
    \hline
    \textbf{format} & 	3	& Transaction type\newline 	
		101: CSR\newline
		other codes other packet formats\\
    \hline
    \textbf{subtype} & 	3	& CSR sub-type\newline
		00: RW\newline	
		01: RS\newline	
		10: RC\newline	
		11: reserved\\	
    \hline
    \textbf{diff} & 0 or 1 & 0: Full address\newline
		1: Differential address\\
    \hline
    \textbf{addr\_msbs} & 6  &	Address[11:6]\\
    \hline
    \textbf{addr\_lsbs} &  6 & Address[5:0] \\
    \hline
  \end{tabulary}
\end{table}

\subsection{diff field} \label{sec:csr-diff}

See section~\ref{sec:loadstore-diff}.

\subsection{operand field} \label{sec:csr-operand}

See section~\ref{sec:atomic-operand}.

\subsection{data\_len and op\_len fields} \label{sec:csr-datalen}

2 bits wide if hart has 32-bit CSRs, 3 bits if 64-bit.  Width of 
\textbf{data} and \textbf{operand} fields respectively.  See section \ref{sec:loadstore-datalen}.


\subsection{addr fields} \label{sec:csr-addr}

The address is split into two parts, with the 6 LSBs output last as these are more likely to compress away.


